{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from collections import defaultdict\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入原始数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "orders_train = pd.read_table('./data/orders_train.txt',sep='\\t')\n",
    "users = pd.read_table('./data/users.txt',sep='\\t')\n",
    "spus = pd.read_table('./data/spus.txt',sep='\\t')\n",
    "pois = pd.read_table('./data/pois.txt',sep='\\t')\n",
    "orders_spu_train = pd.read_table('./data/orders_spu_train.txt',sep='\\t')\n",
    "orders_poi_session = pd.read_table('./data/orders_poi_session.txt',sep='\\t')\n",
    "orders_test_poi = pd.read_table('./data/orders_test_poi.txt',sep='\\t')\n",
    "orders_test_spu = pd.read_table('./data/orders_test_spu.txt',sep='\\t')\n",
    "data = pd.read_table('./data/me/three_week_data.txt',sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>wm_food_spu_id</th>\n",
       "      <th>wm_food_spu_name</th>\n",
       "      <th>price</th>\n",
       "      <th>category</th>\n",
       "      <th>ingredients</th>\n",
       "      <th>taste</th>\n",
       "      <th>stand_food_id</th>\n",
       "      <th>stand_food_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>833366-bf3929-254096</td>\n",
       "      <td>26.0</td>\n",
       "      <td>[0]</td>\n",
       "      <td>[0, 1, 2]</td>\n",
       "      <td>[0]</td>\n",
       "      <td>0.0</td>\n",
       "      <td>833366-bf3929-254096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>032521-a0772-3f3352</td>\n",
       "      <td>20.0</td>\n",
       "      <td>[1]</td>\n",
       "      <td>[3, 1]</td>\n",
       "      <td>[1]</td>\n",
       "      <td>1.0</td>\n",
       "      <td>032521-a0772-3f3352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>952431-c52358-c22123-7f760-832660-fe1862</td>\n",
       "      <td>18.0</td>\n",
       "      <td>[2]</td>\n",
       "      <td>[3, 1, 4, 5, 6, 7]</td>\n",
       "      <td>[2]</td>\n",
       "      <td>2.0</td>\n",
       "      <td>952431-c52358-c22123-7f760-832660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>e84149-6b1215-ca2776-441372-b5649-ac1915-f4338...</td>\n",
       "      <td>10.0</td>\n",
       "      <td>[3]</td>\n",
       "      <td>[8, 9]</td>\n",
       "      <td>[0]</td>\n",
       "      <td>3.0</td>\n",
       "      <td>e84149-6b1215-ca2776-441372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>6b1215-c9725-862243-102951-1f1116</td>\n",
       "      <td>3.58</td>\n",
       "      <td>[4]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>651080-e83605-984171</td>\n",
       "      <td>32.0</td>\n",
       "      <td>[5]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>ff2916-e83605-e363-ca2776</td>\n",
       "      <td>25.0</td>\n",
       "      <td>[6, 7, 8]</td>\n",
       "      <td>[10]</td>\n",
       "      <td>[0]</td>\n",
       "      <td>4.0</td>\n",
       "      <td>e363-ca2776-ff2916-e83605</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>9e3533-b02613-192746-832660-c52358-831763</td>\n",
       "      <td>38.0</td>\n",
       "      <td>[9, 10, 11]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   wm_food_spu_id                                   wm_food_spu_name price  \\\n",
       "0               0                               833366-bf3929-254096  26.0   \n",
       "1               1                                032521-a0772-3f3352  20.0   \n",
       "2               2           952431-c52358-c22123-7f760-832660-fe1862  18.0   \n",
       "3               3  e84149-6b1215-ca2776-441372-b5649-ac1915-f4338...  10.0   \n",
       "4               4                  6b1215-c9725-862243-102951-1f1116  3.58   \n",
       "5               5                               651080-e83605-984171  32.0   \n",
       "6               6                          ff2916-e83605-e363-ca2776  25.0   \n",
       "7               7          9e3533-b02613-192746-832660-c52358-831763  38.0   \n",
       "\n",
       "      category         ingredients taste  stand_food_id  \\\n",
       "0          [0]           [0, 1, 2]   [0]            0.0   \n",
       "1          [1]              [3, 1]   [1]            1.0   \n",
       "2          [2]  [3, 1, 4, 5, 6, 7]   [2]            2.0   \n",
       "3          [3]              [8, 9]   [0]            3.0   \n",
       "4          [4]                 NaN   NaN            NaN   \n",
       "5          [5]                 NaN   NaN            NaN   \n",
       "6    [6, 7, 8]                [10]   [0]            4.0   \n",
       "7  [9, 10, 11]                 NaN   NaN            NaN   \n",
       "\n",
       "                     stand_food_name  \n",
       "0               833366-bf3929-254096  \n",
       "1                032521-a0772-3f3352  \n",
       "2  952431-c52358-c22123-7f760-832660  \n",
       "3        e84149-6b1215-ca2776-441372  \n",
       "4                                NaN  \n",
       "5                                NaN  \n",
       "6          e363-ca2776-ff2916-e83605  \n",
       "7                                NaN  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spus.head(8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一种kg, 菜品被同时购买的图谱\n",
    "* 举个例子，菜品a,b被一个用户购买过，那么a,b之间就有这种图谱关系\n",
    "* 注意1：可能需要设计一个阈值，这个要看最终的统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 存放每个订单购买的菜品\n",
    "order_id_spu = defaultdict(list)\n",
    "for order_id, spu in zip(data['wm_order_id'], data['wm_food_spu_id']):\n",
    "    order_id_spu[order_id].append(spu)\n",
    "for key in order_id_spu:\n",
    "    # 去重，防止有同一个菜品在一个订单中有多个的情况\n",
    "    order_id_spu[key] = list(set(order_id_spu[key]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def zero():\n",
    "    return 0\n",
    "# 存放同时购买的两个菜品出现的频次\n",
    "spu_one_order_num = defaultdict(zero)\n",
    "for order_id in order_id_spu:\n",
    "    order_id_spu[order_id].sort()\n",
    "    for i in range(0,len(order_id_spu[order_id])):\n",
    "        for j in range(i+1,len(order_id_spu[order_id])):\n",
    "            spu_one_order_num[(order_id_spu[order_id][i],order_id_spu[order_id][j])] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([     0., 802659., 272182., 138098.,  84365.,  56802.,  41428.,\n",
       "         30875.,  24348.,  35607.]),\n",
       " array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]),\n",
       " <a list of 10 Patch objects>)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAD8CAYAAACLrvgBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFotJREFUeJzt3XGsXnWd5/H3Z6iM6CxS4EKYFrZMbByRRIUb6IzJZBZcKDqx/CEJZndoSJNuDM7oOMlY5x+yuiaYTIYZEm1CpEPZdcUuo6FRtDZVM9lEkIuyICDpHWToHRhaLSAz7MjgfPeP59edx+tz7/21tD2lfb+SJ8853/P7nd85KeFzzzm/53lSVUiS1ONXhj4ASdJrh6EhSepmaEiSuhkakqRuhoYkqZuhIUnqZmhIkroZGpKkboaGJKnbsqEP4HA788wza9WqVUMfhiS9pjzwwAM/rqqppdodd6GxatUqZmZmhj4MSXpNSfJ3Pe28PSVJ6mZoSJK6GRqSpG6GhiSpm6EhSepmaEiSunWFRpI/SvJIkh8k+UKS1yc5P8l9SXYn+WKSk1vbX23rs237qrH9fLzVH09y5Vh9bavNJtk0Vp84hiRpGEuGRpIVwB8C01V1IXAScC3waeDmqloNPAdsaF02AM9V1ZuBm1s7klzQ+r0NWAt8NslJSU4CPgNcBVwAfKC1ZZExJEkD6L09tQw4Jcky4A3AM8BlwF1t+1bg6ra8rq3Ttl+eJK1+Z1X9rKp+BMwCl7TXbFU9UVUvA3cC61qfhcaQJA1gyU+EV9XfJ/kz4Cng/wLfAB4Anq+qV1qzOWBFW14B7Gl9X0nyAnBGq987tuvxPnvm1S9tfRYa4xck2QhsBDjvvPOW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3zyuuArW15K3D1kRjb0Bg5Yb+uJMkq4J3AfcMeyVHxF8CfAP869IEcJb8B7AP+qt2S+1ySNw59UEdSVf098GfAU8AzwAtV9Y1hj+qoObuqnoHRH4bAWUdiEENjpOvrSo43SX4N+GvgI1X106GP50hK8nvA3qp6YOhjOYqWARcBm6vqncA/cYRuWRwr2n38dcD5wK8Db0zyn4c9quOLoTHS9XUlx5Mkr2MUGJ+vqi8NfTxHwbuA9yV5ktHtx8uS/I9hD+mImwPmqurAVeRdjELkePZu4EdVta+q/gX4EvDbAx/T0fJsknMA2vveIzGIoTFyQn1dSZIwus/9WFX9+dDHczRU1ceramVVrWL07/vNqjqu/wKtqn8A9iR5SytdDjw64CEdDU8Ba5K8of13fjnH+cP/MduB9W15PXD3kRjkmP9E+NFwAn5dybuA3wceTvJgq/1p+/S9ji9/AHy+/TH0BHD9wMdzRFXVfUnuAr7HaJbg9zkOPxme5AvA7wJnJpkDbgRuArYl2cAoPK85ImP7iXBJUi9vT0mSuhkakqRuhoYkqZuhIUnqZmhIkroZGpKkboaGJKmboSFJ6vb/APaCsubNh8shAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(spu_one_order_num_list, range=(0,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 由于存在这种关系的菜品对过多，需要根据菜品对出现的频次进行过滤。本次竞赛中选用25作为这个阈值，如果两种菜品被同时购买出现次数超过25，这种菜品对被保留。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "同时购买比较频繁的组数： 40635\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "save_kg = []\n",
    "for key in spu_one_order_num:\n",
    "    if spu_one_order_num[key] > 25:\n",
    "        save_kg.append(key)\n",
    "        count += 1\n",
    "print('同时购买比较频繁的组数：',count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将这种知识图谱输出到文件中\n",
    "with open('submit_kg_final_2.txt','w') as f:\n",
    "    for tup in save_kg:\n",
    "        f.write(str(tup[0]))\n",
    "        f.write('\\t')\n",
    "        f.write('0')\n",
    "        f.write('\\t')\n",
    "        f.write(str(tup[1]))\n",
    "        f.write('\\n')\n",
    "#         f.write(str(tup[1]))\n",
    "#         f.write('\\t')\n",
    "#         f.write('0')\n",
    "#         f.write('\\t')\n",
    "#         f.write(str(tup[0]))\n",
    "#         f.write('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二种KG：通过所有用户的购买记录，构造菜品属于哪个商家这种关系的图谱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "spu_num = 195244\n",
    "kg_4_dict = {}\n",
    "for poi,spu in zip(data['wm_poi_id'],data['wm_food_spu_id']):\n",
    "    kg_4_dict[(spu,poi)] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('submit_kg_final_4.txt','w') as f:\n",
    "    for tup in kg_4_dict:\n",
    "        f.write(str(tup[0]))\n",
    "        f.write('\\t')\n",
    "        f.write('0')\n",
    "        f.write('\\t')\n",
    "        f.write(str(tup[1]+spu_num))\n",
    "        f.write('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "entity的数量： 198821\n"
     ]
    }
   ],
   "source": [
    "# 统计实际有多少kg entity\n",
    "kg_4 = pd.read_table('submit_kg_final_4.txt', header=None)\n",
    "\n",
    "entity_num = max(max(kg_4[0]), max(kg_4[2])) + 1\n",
    "print('entity的数量：', entity_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三种KG：将上面两种kg进行组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_2 = pd.read_table('submit_kg_final_2.txt',header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_2[1] = kg_2[1]+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_5 = pd.concat([kg_2,kg_4],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_5.to_csv('submit_kg_final_5.txt',sep='\\t',header=None,index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**这个程序最终的输出是submit_kg_final_5.txt，它的格式是每行为【 菜品 关系 其他实体 】的三元组，代表一种知识图谱。作为后续GNN模型的输入文件**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
